﻿using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.Models;

namespace WMSProject.ChuBoWen
{
    /// <summary>
    /// 单位管理信息服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "楚博文")]
    public class UnitsManagerService : ApplicationService, IUnitsManagerRepository
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IRepository<UnitsManager, int> _unitsmanagerRepository;
        public UnitsManagerService(IRepository<UnitsManager, int> unitsmanagerRepository)
        {
            _unitsmanagerRepository = unitsmanagerRepository;
        }

        /// <summary>
        /// 获取单位管理信息
        /// </summary>
        /// <param name="unitNumber">单位编码</param>
        /// <param name="unitName">单位名称</param>
        /// <returns>返回单位管理信息的泛型集合</returns>
        public async Task<InterSelect<object>> GetUnitsManagerList(string? unitNumber, string? unitName, int pageIndex, int pageSize)
        {
            var unitsManagers = await _unitsmanagerRepository.GetListAsync();
            if (!string.IsNullOrEmpty(unitNumber))
            {
                unitsManagers = unitsManagers.Where(p => p.UnitNumber.Contains(unitNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(unitName))
            {
                unitsManagers = unitsManagers.Where(p => p.UnitName.Contains(unitName)).ToList();
            }
            var totalCount = unitsManagers.Count;
            var pageList = unitsManagers.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var productClassificationList = AutoMapperExt.MapToList<UnitsManager>(pageList);
            var data = new { TotalCount = totalCount, PageList = productClassificationList };
            if (unitsManagers != null)
            {
                return new InterSelect<object>(true, "0", data);
            }
            return new InterSelect<object>(false, "1", null);
        }

        /// <summary>
        /// 新增单位
        /// </summary>
        /// <param name="unitsManager"></param>
        /// <returns></returns>
        public async Task<int> AddUnitsManager(UnitsManager unitsManager)
        {
            var result = await _unitsmanagerRepository.FindAsync(x => x.UnitName == unitsManager.UnitName);
            if (result == null)
            {
                await _unitsmanagerRepository.InsertAsync(unitsManager);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 删除单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteUnitsManager(int id)
        {
            var result = await _unitsmanagerRepository.FindAsync(id);
            if (result != null)
            {
                await _unitsmanagerRepository.DeleteAsync(id);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 获取单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<UnitsManager> GetUnitsManager(int id)
        {
            var result = await _unitsmanagerRepository.FindAsync(id);
            if (result != null)
            {
                return result;
            }
            return null;
        }

        /// <summary>
        /// 修改单位
        /// </summary>
        /// <param name="unitsManagerDTO"></param>
        /// <returns></returns>
        public async Task<int> UpdateUnitsManager(UnitsManagerDTO unitsManagerDTO)
        {
            var result = await _unitsmanagerRepository.GetAsync(unitsManagerDTO.Id);
            result.UnitNumber = unitsManagerDTO.UnitNumber;
            result.UnitName = unitsManagerDTO.UnitName;
            result.Remark = unitsManagerDTO.Remark;
            await _unitsmanagerRepository.UpdateAsync(result);
            return result != null ? 1 : 0;
        }
    }
}
